<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <script src="elementUI/vue.js"></script>
    <script src="elementUI/echarts.js"></script>
    <script src="elementUI/vue-echarts.js"></script>
    <script src="elementUI/axios.min.js"></script>
    <title>echarts</title>
</head>
<body>
<div id="app">
    <button @click="changeData()">获取数据</button>
    <div id="main" style="height:400px"></div>
</div>


<script>
    let myChart=null;
    new Vue({

        el:'#app',
        data(){
            return{
                option : {
                    title: {
                        text: '某站点用户访问来源',
                        subtext: '纯属虚构',
                        left: 'center'
                    },
                    tooltip: {
                        trigger: 'item'
                    },
                    legend: {
                        orient: 'vertical',
                        left: 'left',
                    },
                    series: [
                        {
                            name: '访问来源',
                            type: 'pie',
                            radius: '50%',
                            data: [
                                {value: 1048, name: '搜索引擎'},
                                {value: 735, name: '直接访问'},
                                {value: 580, name: '邮件营销'},
                                {value: 484, name: '联盟广告'},
                                {value: 300, name: '视频广告'}
                            ],
                            emphasis: {
                                itemStyle: {
                                    shadowBlur: 10,
                                    shadowOffsetX: 0,
                                    shadowColor: 'rgba(0, 0, 0, 0.5)'
                                }
                            }
                        }
                    ]
                }

                }
        },
        mounted(){
            myChart = echarts.init(document.getElementById('main'));
            myChart.setOption(this.option);
        },
        methods:{
            changeData:function (){
                let _this=this;
                axios.get('http://localhost:8090/api/car/countType')
                    .then(function (response) {
                        let list=response.data.results;
                        //console.log(response.data.results);
                        let a=new Array();
                        for (let i = 0; i < list.length; i++) {
                            a.push(list[i].name);
                        }
                        _this.option.legend.data=a;
                        _this.option.series[0].data=list;
                    })
                    .catch(function (error) {
                        // handle error
                        console.log(error);
                    })
                    .then(function () {
                        // always executed
                    });
                myChart.setOption(this.option);
            }
        }
    });
</script>
</body>
</html>